package com.qy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

import com.qy.dao.UserInfoDao;

import java.util.List;
import java.util.Map;
import java.sql.Types;

import com.qy.db.DbParameter;

import java.util.HashMap;
import java.util.ArrayList;

import com.qy.db.pool.DBPoolMgr;
import com.qy.bean.Option;
import com.qy.bean.UserInfo;
import com.qy.db.BaseDao;

/**
* u_user_info daoImpl实现类的生成
* @author zhangwenlong
* @date Wed Jan 31 11:37:49 GMT+08:00 2018
*/ 
public class UserInfoDaoImpl extends BaseDao  implements UserInfoDao {

	@Override
	public boolean createUserInfo(UserInfo userInfo) {
		boolean result = false;
		int id = -1;
		if (userInfo.beginAdd()) {
			String sql = "insert into u_user_info (id,image,user_name,sex,third_id,phone,create_date,login_date,login_ip,user_type,user_status,tmp_secret) values (?,?,?,?,?,?,?,?,?,?,?,?);";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, userInfo.getId()));
			param.put(2, new DbParameter(Types.VARCHAR, userInfo.getImage()));
			param.put(3, new DbParameter(Types.VARCHAR, userInfo.getUserName()));
			param.put(4, new DbParameter(Types.INTEGER, userInfo.getSex()));
			param.put(5, new DbParameter(Types.VARCHAR, userInfo.getThirdId()));
			param.put(6, new DbParameter(Types.VARCHAR, userInfo.getPhone()));
			param.put(7, new DbParameter(Types.TIMESTAMP, userInfo.getCreateDate()));
			param.put(8, new DbParameter(Types.TIMESTAMP, userInfo.getLoginDate()));
			param.put(9, new DbParameter(Types.VARCHAR, userInfo.getLoginIp()));
			param.put(10, new DbParameter(Types.INTEGER, userInfo.getUserType()));
			param.put(11, new DbParameter(Types.INTEGER, userInfo.getUserStatus()));
			param.put(12, new DbParameter(Types.VARCHAR, userInfo.getTmpSecret()));
			id = execLastId(sql, param);
			result = id > -1;
			userInfo.commitAdd(result);
			userInfo.setId(id);
		}
		return result;
	}

	@Override
	public List<UserInfo> getUserInfoList() {
		String sql = "select * from u_user_info;";
		PreparedStatement pstmt = execQuery(sql, null);
		ResultSet rs = null;
		List<UserInfo> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<UserInfo>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public UserInfo getUserInfoById(int id) {
		String sql = "select * from u_user_info where id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, id));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		UserInfo userInfo = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					userInfo = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return userInfo;
	}

	@Override
	public boolean updateUserInfo(UserInfo userInfo) {
		boolean result = false;
		if (userInfo.beginUpdate()) {
			String sql = "update u_user_info set image=?,user_name=?,sex=?,third_id=?,phone=?,create_date=?,login_date=?,login_ip=?,user_type=?,user_status=?,tmp_secret=? where id=?;";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.VARCHAR, userInfo.getImage()));
			param.put(2, new DbParameter(Types.VARCHAR, userInfo.getUserName()));
			param.put(3, new DbParameter(Types.INTEGER, userInfo.getSex()));
			param.put(4, new DbParameter(Types.VARCHAR, userInfo.getThirdId()));
			param.put(5, new DbParameter(Types.VARCHAR, userInfo.getPhone()));
			param.put(6, new DbParameter(Types.TIMESTAMP, userInfo.getCreateDate()));
			param.put(7, new DbParameter(Types.TIMESTAMP, userInfo.getLoginDate()));
			param.put(8, new DbParameter(Types.VARCHAR, userInfo.getLoginIp()));
			param.put(9, new DbParameter(Types.INTEGER, userInfo.getUserType()));
			param.put(10, new DbParameter(Types.INTEGER, userInfo.getUserStatus()));
			param.put(11, new DbParameter(Types.VARCHAR, userInfo.getTmpSecret()));
			param.put(12, new DbParameter(Types.INTEGER, userInfo.getId()));
			result = execNoneQuery(sql, param) > -1;
			userInfo.commitUpdate(result);
		}
		return result;
	}

	public UserInfo resultTo(ResultSet rs) throws SQLException {
		UserInfo userInfo = new UserInfo();
		userInfo.setId(rs.getInt("id"));
		userInfo.setImage(rs.getString("image"));
		userInfo.setUserName(rs.getString("user_name"));
		userInfo.setSex(rs.getInt("sex"));
		userInfo.setThirdId(rs.getString("third_id"));
		userInfo.setPhone(rs.getString("phone"));
		userInfo.setCreateDate(rs.getTimestamp("create_date"));
		userInfo.setLoginDate(rs.getTimestamp("login_date"));
		userInfo.setLoginIp(rs.getString("login_ip"));
		userInfo.setUserType(rs.getInt("user_type"));
		userInfo.setUserStatus(rs.getInt("user_status"));
		userInfo.setTmpSecret(rs.getString("tmp_secret"));
		userInfo.setOp(Option.None);
		return userInfo;
	}

	@Override
	public Connection openConn(){
		return DBPoolMgr.getStrategyConn();
	}

	@Override
	public UserInfo getUserIdByOpenid(String openid) {
		String sql = "select * from u_user_info where third_id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.VARCHAR, openid));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		UserInfo userInfo = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					userInfo = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return userInfo;
	}

}

